package com.zjitc.chapter12.controller;

import com.zjitc.chapter12.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
public class PageController {
    @RequestMapping("/hello")
    public void helloByVoid() {
        System.out.println("PageController.hello");
    }
    @RequestMapping("/showPageByString")
    public String showPageByString() {
        System.out.println("PageController.showPageByString");
        return "hello";
    }
    @RequestMapping("/showPageByForward")
    public String showPageByForward() {
        System.out.println("PageController.showPageByForward");
        return "forward:orders.jsp";
    }
    @RequestMapping("/showPageByRedirect")
    public String showPageByRedirect() {
        System.out.println("PageController.showPageByRedirect");
        return "redirect:http://www.zjitc.edu.cn";
    }
    @RequestMapping("/showPageByRequest")
    public String showPageByRequest(HttpServletRequest request) {
        System.out.println("PageController.showPageByRequest");
        request.setAttribute("name", "zjitc");
        return "hello";
    }
    @RequestMapping("/showPageByModel")
    public String showPageByModel(Model model) {
        System.out.println("PageController.showPageByModel");
        model.addAttribute("name", "zjitc");
        User user = new User();
        user.setPassword("1333");
        model.addAttribute("user", user);
        return "hello";
    }
    @RequestMapping("/showModelAndView")
    public ModelAndView showModelAndView() {
        System.out.println("PageController.showModelAndView");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("name", "zjitc");
        User user = new User();
        user.setPassword("1333");
        modelAndView.addObject("user", user);
        modelAndView.setViewName("hello");
        return modelAndView;
    }
}
